package ibmetp.mis.controller.bulletin;

import ibmetp.mis.common.Constants;
import ibmetp.mis.common.Message;
import ibmetp.mis.service.BulletinService;
import ibmetp.mis.serviceImpl.BulletinServiceImpl;
import ibmetp.mis.vo.Bulletin;
import ibmetp.mis.vo.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public class BulletinAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BulletinAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODING_UTF8);
		System.out.println("Call BulletinAddservlet...");
		//获取参数
		Bulletin bulletin = new Bulletin();
		bulletin.setPublishMsg(request.getParameter("publishMsg"));
		User user=(User)request.getSession().getAttribute("userinfo");
		if(user!=null)
		bulletin.setPrincipal(user.getUserName());
		    
		BulletinService  bl=new BulletinServiceImpl();

	    String result=bl.addBulletin(bulletin);
	  //如果有返回消息，则将消息发回页面
		if(result!=null){		
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().print(JSONObject.fromObject(new Message(result)));
		}
	    
	}

}
